package com.it.selfAfter.entity.base;


public class Response<T> {

    /**
     * 是否成功(ture 或 false)
     *
     * @mock true
     */
    private boolean success;

    /**
     * 返回状态编码
     *
     * @mock 200
     */
    private int code;

    /**
     * 返回信息(备注)
     */
    private Object msg;

    /**
     * 返回数据
     */
    private T data;

    public Response() {

    }

    public Response(int code, Object status) {
        super();
        this.code = code;
        this.msg = status;
        if (code == 1) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public Response(int code, String status, T result) {
        super();
        this.code = code;
        this.msg = status;
        this.data = result;
        if (code == 1) {
            this.success = true;
        } else {
            this.success = false;
        }
    }

    public static Response<?> ok() {
        return new Response<>(1, "success");
    }

    public static <T> Response<T> ok(T t) {
        return new Response<T>(1, "success", t);
    }

    public static <T> Response<T> ok(T t, String message) {
        return new Response<T>(1, message, t);
    }

    public static Response<?> error(String status) {
        return new Response<>(500, status);
    }

    public static Response<?> error(int code, String status) {
        return new Response<>(code, status);
    }

    public static Response<?> error(ErrorCode errorCode) {
        return new Response<>(Integer.valueOf(errorCode.getErrorCode()), errorCode.getMessage());
    }

    public static Response<?> error(Exception e) {
        return new Response<>(500, e);
    }

    public static <T> Response<T> builder(int code, String status, T t) {
        return new Response<T>(code, status, t);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Object getMsg() {
        return msg;
    }

    public void setMsg(Object msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
